using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Data;
using QL_Truong_Hoc.DTO;

namespace QL_Truong_Hoc.DAO
{
	public static class TDanhSachDuTuyenDAO
	{
		#region Methods

		/// <summary>
		/// Saves a record to the tDanhSachDuTuyen table.
		/// </summary>
		public static bool Insert(Database objDb, DbTransaction objTrans, TDanhSachDuTuyenDTO tDanhSachDuTuyen)
		{
bool blnResult = false;
try
{
			clsValidationUtility.ValidateArgument("tDanhSachDuTuyen", tDanhSachDuTuyen);

			object[] parameters = new object[]
			{
				Null.GetNull(tDanhSachDuTuyen.HS_ID, DBNull.Value),
				Null.GetNull(tDanhSachDuTuyen.TD_ID, DBNull.Value),
				Null.GetNull(tDanhSachDuTuyen.NG_ID1, DBNull.Value),
				Null.GetNull(tDanhSachDuTuyen.NG_ID2, DBNull.Value),
				Null.GetNull(tDanhSachDuTuyen.NamTuyenSinh, DBNull.Value),
				Null.GetNull(tDanhSachDuTuyen.DotTuyenSinh, ""),
				Null.GetNull(tDanhSachDuTuyen.TuyenSinh, false),
				Null.GetNull(tDanhSachDuTuyen.NhapHoc, false),
				Null.GetNull(tDanhSachDuTuyen.NG_ID, DBNull.Value),
				Null.GetNull(tDanhSachDuTuyen.PhieuHocNghe, false),
				Null.GetNull(tDanhSachDuTuyen.TheHocNghe, false),
				Null.GetNull(tDanhSachDuTuyen.QuyetDinhXuatNgu, false),
				Null.GetNull(tDanhSachDuTuyen.PhieuQuanNhan, false),
				Null.GetNull(tDanhSachDuTuyen.CMND, false),
				Null.GetNull(tDanhSachDuTuyen.BangVanHoa, false),
				Null.GetNull(tDanhSachDuTuyen.BanSaoGiayKhaiSinh, false),
				Null.GetNull(tDanhSachDuTuyen.Anh, false),
				Null.GetNull(tDanhSachDuTuyen.DaCapVPP, false),
				Null.GetNull(tDanhSachDuTuyen.QuyetDinhXuatNgu_So, ""),
				Null.GetNull(tDanhSachDuTuyen.QuyetDinhXuatNgu_Ngay, DBNull.Value),
				Null.GetNull(tDanhSachDuTuyen.TheHocNghe_So, ""),
				Null.GetNull(tDanhSachDuTuyen.TheHocNghe_Ngay, DBNull.Value)
			};

int intResult;
if (objTrans == null)
{ intResult = objDb.ExecuteNonQuery("tDanhSachDuTuyenInsert", parameters); }
else if (objTrans.Connection == null)
{ intResult = objDb.ExecuteNonQuery("tDanhSachDuTuyenInsert", parameters); }
else
{ intResult = objDb.ExecuteNonQuery(objTrans, "tDanhSachDuTuyenInsert", parameters); }
if (intResult > 0) return true;
}
catch { return false; }
return blnResult;
		}

		/// <summary>
		/// Updates a record in the tDanhSachDuTuyen table.
		/// </summary>
		public static bool Update(Database objDb, DbTransaction objTrans, TDanhSachDuTuyenDTO tDanhSachDuTuyen)
		{
bool blnResult = false;
try
{
			clsValidationUtility.ValidateArgument("tDanhSachDuTuyen", tDanhSachDuTuyen);

			object[] parameters = new object[]
			{
				Null.GetNull(tDanhSachDuTuyen.HS_ID, DBNull.Value),
				Null.GetNull(tDanhSachDuTuyen.TD_ID, DBNull.Value),
				Null.GetNull(tDanhSachDuTuyen.NG_ID1, DBNull.Value),
				Null.GetNull(tDanhSachDuTuyen.NG_ID2, DBNull.Value),
				Null.GetNull(tDanhSachDuTuyen.NamTuyenSinh, DBNull.Value),
				Null.GetNull(tDanhSachDuTuyen.DotTuyenSinh, ""),
				Null.GetNull(tDanhSachDuTuyen.TuyenSinh, false),
				Null.GetNull(tDanhSachDuTuyen.NhapHoc, false),
				Null.GetNull(tDanhSachDuTuyen.NG_ID, DBNull.Value),
				Null.GetNull(tDanhSachDuTuyen.PhieuHocNghe, false),
				Null.GetNull(tDanhSachDuTuyen.TheHocNghe, false),
				Null.GetNull(tDanhSachDuTuyen.QuyetDinhXuatNgu, false),
				Null.GetNull(tDanhSachDuTuyen.PhieuQuanNhan, false),
				Null.GetNull(tDanhSachDuTuyen.CMND, false),
				Null.GetNull(tDanhSachDuTuyen.BangVanHoa, false),
				Null.GetNull(tDanhSachDuTuyen.BanSaoGiayKhaiSinh, false),
				Null.GetNull(tDanhSachDuTuyen.Anh, false),
				Null.GetNull(tDanhSachDuTuyen.DaCapVPP, false),
				Null.GetNull(tDanhSachDuTuyen.QuyetDinhXuatNgu_So, ""),
				Null.GetNull(tDanhSachDuTuyen.QuyetDinhXuatNgu_Ngay, DBNull.Value),
				Null.GetNull(tDanhSachDuTuyen.TheHocNghe_So, ""),
				Null.GetNull(tDanhSachDuTuyen.TheHocNghe_Ngay, DBNull.Value)
			};

int intResult;
if (objTrans == null)
{ intResult = objDb.ExecuteNonQuery("tDanhSachDuTuyenUpdate", parameters); }
else if (objTrans.Connection == null)
{ intResult = objDb.ExecuteNonQuery("tDanhSachDuTuyenUpdate", parameters); }
else
{ intResult = objDb.ExecuteNonQuery(objTrans, "tDanhSachDuTuyenUpdate", parameters); }
if (intResult > 0) return true;
}
catch { return false; }
return blnResult;
		}

		/// <summary>
		/// Deletes a record from the tDanhSachDuTuyen table by its primary key.
		/// </summary>
		public static bool Delete(Database objDb, DbTransaction objTrans, int hS_ID)
		{
bool blnResult = false;
try
{
			object[] parameters = new object[]
			{
Null.GetNull(hS_ID, DBNull.Value)
			};

int intResult;
if (objTrans == null)
{ intResult = objDb.ExecuteNonQuery("tDanhSachDuTuyenDelete", parameters); }
else if (objTrans.Connection == null)
{ intResult = objDb.ExecuteNonQuery("tDanhSachDuTuyenDelete", parameters); }
else
{ intResult = objDb.ExecuteNonQuery(objTrans, "tDanhSachDuTuyenDelete", parameters); }
if (intResult > 0) return true;
}
catch { return false; }
return blnResult;
		}

		/// <summary>
		/// Deletes a record from the tDanhSachDuTuyen table by its primary key.
		/// </summary>
		public static bool Delete(Database objDb, DbTransaction objTrans, string strWhere)		{
bool blnResult = false;
try
{
			object[] parameters = new object[]
			{strWhere
			};

int intResult;
if (objTrans == null)
{ intResult = objDb.ExecuteNonQuery("tDanhSachDuTuyenDeleteWhere", parameters); }
else if (objTrans.Connection == null)
{ intResult = objDb.ExecuteNonQuery("tDanhSachDuTuyenDeleteWhere", parameters); }
else
{ intResult = objDb.ExecuteNonQuery(objTrans, "tDanhSachDuTuyenDeleteWhere", parameters); }
if (intResult > 0) return true;
}
catch { return false; }
return blnResult;
		}

		/// <summary>
		/// Deletes a record from the tDanhSachDuTuyen table by a foreign key.
		/// </summary>
		public static bool DeleteAllByHS_ID(Database objDb, DbTransaction objTrans, int hS_ID)
		{
bool blnResult = false;
try
{
			object[] parameters = new object[]
			{
Null.GetNull(hS_ID, DBNull.Value)
			};

int intResult;
if (objTrans == null)
{ intResult = objDb.ExecuteNonQuery("tDanhSachDuTuyenDeleteAllByHS_ID", parameters); }
else if (objTrans.Connection == null)
{ intResult = objDb.ExecuteNonQuery("tDanhSachDuTuyenDeleteAllByHS_ID", parameters); }
else
{ intResult = objDb.ExecuteNonQuery(objTrans, "tDanhSachDuTuyenDeleteAllByHS_ID", parameters); }
if (intResult > 0) return true;
}
catch { return false; }
return blnResult;
		}

		/// <summary>
		/// Selects a single record from the tDanhSachDuTuyen table.
		/// </summary>
		public static TDanhSachDuTuyenDTO Select(Database objDb, int hS_ID)
		{
try{
			object[] parameters = new object[]
			{
Null.GetNull(hS_ID, DBNull.Value)
			};

			using (IDataReader dataReader = objDb.ExecuteReader("tDanhSachDuTuyenSelect", parameters))
			{
				if(dataReader.FieldCount > 0)
				{
					return MapDataReader(dataReader);
				}
				else
				{
					return null;
				}
			}
}catch { return null; }
		}

		/// <summary>
		/// Selects a single record from the tDanhSachDuTuyen table.
		/// </summary>
		public static DataTable Select(Database objDb, int hS_ID, bool blnReturnDatatable = true)
		{
try{
			object[] parameters = new object[]
			{
Null.GetNull(hS_ID, DBNull.Value)
			};

			using (IDataReader dataReader = objDb.ExecuteReader("tDanhSachDuTuyenSelect", parameters))
			{
				if(dataReader.FieldCount > 0)
				{
				DataTable dt = new DataTable();
				dt.Load(dataReader);
					return dt;
				}
				else
				{
					return null;
				}
			}
}catch { return null; }
		}

		/// <summary>
		/// Get next index from the tDanhSachDuTuyen table.
		/// </summary>
		public static int SelectNextIndex(Database objDb)		{
int intResult = -1;
try{
IDataReader dr = objDb.ExecuteReader("tDanhSachDuTuyenSelectNextIndex");
if (dr != null)
{
DataTable dt = Untils.ConvertDataReaderToDataTable(dr, true);
intResult = clsCommon.fncCnvNullToInt(dt.Rows[0][0]);
}
}catch { }
return intResult;
		}

		/// <summary>
		/// Selects all records from the tDanhSachDuTuyen table.
		/// </summary>
		public static List<TDanhSachDuTuyenDTO> SelectAll(Database objDb)
		{
try{
			using (IDataReader dataReader = objDb.ExecuteReader("tDanhSachDuTuyenSelectAll"))
			{

				return MapDataReaderList(dataReader);
			}
}catch { return null; }
		}

		/// <summary>
		/// Selects all records from the tDanhSachDuTuyen table.
		/// </summary>
		public static List<TDanhSachDuTuyenDTO> SelectWhere(Database objDb, string strWhere)
		{
try{
			object[] parameters = new object[]
			{strWhere
			};

			using (IDataReader dataReader = objDb.ExecuteReader("tDanhSachDuTuyenSelectWhere", parameters))
			{

				return MapDataReaderList(dataReader);
			}
}catch { return null; }
		}

		/// <summary>
		/// Selects all records from the tDanhSachDuTuyen table.
		/// </summary>
		public static DataTable SelectAll(Database objDb, bool blnReturnDatatable = true)
		{
try{
			using (IDataReader dataReader = objDb.ExecuteReader("tDanhSachDuTuyenSelectAll"))
			{

				if(dataReader.FieldCount > 0)
				{
				DataTable dt = new DataTable();
				dt.Load(dataReader);
					return dt;
				}
				else
				{
					return null;
				}
			}
}catch { return null; }
		}

		/// <summary>
		/// Selects all records from the tDanhSachDuTuyen table.
		/// </summary>
		public static DataTable SelectWhere(Database objDb, string strWhere, bool blnReturnDatatable = true)
		{
try{
			object[] parameters = new object[]
			{strWhere
			};

			using (IDataReader dataReader = objDb.ExecuteReader("tDanhSachDuTuyenSelectWhere", parameters))
			{

				if(dataReader.FieldCount > 0)
				{
				DataTable dt = new DataTable();
				dt.Load(dataReader);
					return dt;
				}
				else
				{
					return null;
				}
			}
}catch { return null; }
		}

		/// <summary>
		/// Selects all records from the tDanhSachDuTuyen table by a foreign key.
		/// </summary>
		public static List<TDanhSachDuTuyenDTO> SelectAllByHS_ID(Database objDb, int hS_ID)
		{
try{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@HS_ID", hS_ID)
			};

			using (IDataReader dataReader = objDb.ExecuteReader(objDb.ConnectionString, CommandType.StoredProcedure, "tDanhSachDuTuyenSelectAllByHS_ID", parameters))
			{

				return MapDataReaderList(dataReader);
			}
}catch { return null; }
		}

		/// <summary>
		/// Selects all records from the tDanhSachDuTuyen table by a foreign key.
		/// </summary>
		public static DataTable SelectAllByHS_ID(Database objDb, int hS_ID, bool blnReturnDatatable = true)
		{
try{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@HS_ID", hS_ID)
			};

			using (IDataReader dataReader = objDb.ExecuteReader(objDb.ConnectionString, CommandType.StoredProcedure, "tDanhSachDuTuyenSelectAllByHS_ID", parameters))
			{

				if(dataReader.FieldCount > 0)
				{
				DataTable dt = new DataTable();
				dt.Load(dataReader);
					return dt;
				}
				else
				{
					return null;
				}
			}
}catch { return null; }
		}

		/// <summary>
		/// Creates a new instance of the TDanhSachDuTuyenDTO class and populates it with data from the specified SqlDataReader.
		/// </summary>
		private static TDanhSachDuTuyenDTO MapDataReader(IDataReader dataReader)
		{
			TDanhSachDuTuyenDTO tDanhSachDuTuyenDTO = new TDanhSachDuTuyenDTO();
DataTable dt = new DataTable();
dt.Load(dataReader);
if (dt.Rows.Count <= 0) return tDanhSachDuTuyenDTO;
			tDanhSachDuTuyenDTO.HS_ID = clsCommon.fncCnvNullToInt(dt.Rows[0]["HS_ID"]);
			tDanhSachDuTuyenDTO.TD_ID = clsCommon.fncCnvNullToInt(dt.Rows[0]["TD_ID"]);
			tDanhSachDuTuyenDTO.NG_ID1 = clsCommon.fncCnvNullToInt(dt.Rows[0]["NG_ID1"]);
			tDanhSachDuTuyenDTO.NG_ID2 = clsCommon.fncCnvNullToInt(dt.Rows[0]["NG_ID2"]);
			tDanhSachDuTuyenDTO.NamTuyenSinh = clsCommon.fncCnvNullToInt(dt.Rows[0]["NamTuyenSinh"]);
			tDanhSachDuTuyenDTO.DotTuyenSinh = clsCommon.fncCnvNullToString(dt.Rows[0]["DotTuyenSinh"]);
			tDanhSachDuTuyenDTO.TuyenSinh = clsCommon.fncCnvNullToBool(dt.Rows[0]["TuyenSinh"]);
			tDanhSachDuTuyenDTO.NhapHoc = clsCommon.fncCnvNullToBool(dt.Rows[0]["NhapHoc"]);
			tDanhSachDuTuyenDTO.NG_ID = clsCommon.fncCnvNullToInt(dt.Rows[0]["NG_ID"]);
			tDanhSachDuTuyenDTO.PhieuHocNghe = clsCommon.fncCnvNullToBool(dt.Rows[0]["PhieuHocNghe"]);
			tDanhSachDuTuyenDTO.TheHocNghe = clsCommon.fncCnvNullToBool(dt.Rows[0]["TheHocNghe"]);
			tDanhSachDuTuyenDTO.QuyetDinhXuatNgu = clsCommon.fncCnvNullToBool(dt.Rows[0]["QuyetDinhXuatNgu"]);
			tDanhSachDuTuyenDTO.PhieuQuanNhan = clsCommon.fncCnvNullToBool(dt.Rows[0]["PhieuQuanNhan"]);
			tDanhSachDuTuyenDTO.CMND = clsCommon.fncCnvNullToBool(dt.Rows[0]["CMND"]);
			tDanhSachDuTuyenDTO.BangVanHoa = clsCommon.fncCnvNullToBool(dt.Rows[0]["BangVanHoa"]);
			tDanhSachDuTuyenDTO.BanSaoGiayKhaiSinh = clsCommon.fncCnvNullToBool(dt.Rows[0]["BanSaoGiayKhaiSinh"]);
			tDanhSachDuTuyenDTO.Anh = clsCommon.fncCnvNullToBool(dt.Rows[0]["Anh"]);
			tDanhSachDuTuyenDTO.DaCapVPP = clsCommon.fncCnvNullToBool(dt.Rows[0]["DaCapVPP"]);
			tDanhSachDuTuyenDTO.QuyetDinhXuatNgu_So = clsCommon.fncCnvNullToString(dt.Rows[0]["QuyetDinhXuatNgu_So"]);
			tDanhSachDuTuyenDTO.QuyetDinhXuatNgu_Ngay = clsCommon.fncCnvNullToDatetime(dt.Rows[0]["QuyetDinhXuatNgu_Ngay"]);
			tDanhSachDuTuyenDTO.TheHocNghe_So = clsCommon.fncCnvNullToString(dt.Rows[0]["TheHocNghe_So"]);
			tDanhSachDuTuyenDTO.TheHocNghe_Ngay = clsCommon.fncCnvNullToDatetime(dt.Rows[0]["TheHocNghe_Ngay"]);

			return tDanhSachDuTuyenDTO;
		}
		/// <summary>
		/// Creates a new instance of the TDanhSachDuTuyenDTO class and populates it with data from the specified SqlDataReader.
		/// </summary>
		private static List<TDanhSachDuTuyenDTO> MapDataReaderList(IDataReader dataReader)
		{
				List<TDanhSachDuTuyenDTO> lstResult = new List<TDanhSachDuTuyenDTO>();
			TDanhSachDuTuyenDTO tDanhSachDuTuyenDTO;
DataTable dt = new DataTable();
dt.Load(dataReader);
for (int i = 0; i <= dt.Rows.Count-1; i++)
{
			tDanhSachDuTuyenDTO = new TDanhSachDuTuyenDTO();
			tDanhSachDuTuyenDTO.HS_ID = clsCommon.fncCnvNullToInt(dt.Rows[i]["HS_ID"]);
			tDanhSachDuTuyenDTO.TD_ID = clsCommon.fncCnvNullToInt(dt.Rows[i]["TD_ID"]);
			tDanhSachDuTuyenDTO.NG_ID1 = clsCommon.fncCnvNullToInt(dt.Rows[i]["NG_ID1"]);
			tDanhSachDuTuyenDTO.NG_ID2 = clsCommon.fncCnvNullToInt(dt.Rows[i]["NG_ID2"]);
			tDanhSachDuTuyenDTO.NamTuyenSinh = clsCommon.fncCnvNullToInt(dt.Rows[i]["NamTuyenSinh"]);
			tDanhSachDuTuyenDTO.DotTuyenSinh = clsCommon.fncCnvNullToString(dt.Rows[i]["DotTuyenSinh"]);
			tDanhSachDuTuyenDTO.TuyenSinh = clsCommon.fncCnvNullToBool(dt.Rows[i]["TuyenSinh"]);
			tDanhSachDuTuyenDTO.NhapHoc = clsCommon.fncCnvNullToBool(dt.Rows[i]["NhapHoc"]);
			tDanhSachDuTuyenDTO.NG_ID = clsCommon.fncCnvNullToInt(dt.Rows[i]["NG_ID"]);
			tDanhSachDuTuyenDTO.PhieuHocNghe = clsCommon.fncCnvNullToBool(dt.Rows[i]["PhieuHocNghe"]);
			tDanhSachDuTuyenDTO.TheHocNghe = clsCommon.fncCnvNullToBool(dt.Rows[i]["TheHocNghe"]);
			tDanhSachDuTuyenDTO.QuyetDinhXuatNgu = clsCommon.fncCnvNullToBool(dt.Rows[i]["QuyetDinhXuatNgu"]);
			tDanhSachDuTuyenDTO.PhieuQuanNhan = clsCommon.fncCnvNullToBool(dt.Rows[i]["PhieuQuanNhan"]);
			tDanhSachDuTuyenDTO.CMND = clsCommon.fncCnvNullToBool(dt.Rows[i]["CMND"]);
			tDanhSachDuTuyenDTO.BangVanHoa = clsCommon.fncCnvNullToBool(dt.Rows[i]["BangVanHoa"]);
			tDanhSachDuTuyenDTO.BanSaoGiayKhaiSinh = clsCommon.fncCnvNullToBool(dt.Rows[i]["BanSaoGiayKhaiSinh"]);
			tDanhSachDuTuyenDTO.Anh = clsCommon.fncCnvNullToBool(dt.Rows[i]["Anh"]);
			tDanhSachDuTuyenDTO.DaCapVPP = clsCommon.fncCnvNullToBool(dt.Rows[i]["DaCapVPP"]);
			tDanhSachDuTuyenDTO.QuyetDinhXuatNgu_So = clsCommon.fncCnvNullToString(dt.Rows[i]["QuyetDinhXuatNgu_So"]);
			tDanhSachDuTuyenDTO.QuyetDinhXuatNgu_Ngay = clsCommon.fncCnvNullToDatetime(dt.Rows[i]["QuyetDinhXuatNgu_Ngay"]);
			tDanhSachDuTuyenDTO.TheHocNghe_So = clsCommon.fncCnvNullToString(dt.Rows[i]["TheHocNghe_So"]);
			tDanhSachDuTuyenDTO.TheHocNghe_Ngay = clsCommon.fncCnvNullToDatetime(dt.Rows[i]["TheHocNghe_Ngay"]);
lstResult.Add(tDanhSachDuTuyenDTO);
}

			return lstResult;
		}

		#endregion
	}
}
